package com.example.noobpackage.ui.register

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.*
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.example.noobpackage.R
import com.google.android.material.textfield.TextInputLayout
import kotlinx.android.synthetic.main.fragment_register.*


class RegisterFragment : Fragment() {
    private val viewModel: RegisterViewModel by viewModels()

    val SUCCESS = 1
    val ACCOUNT_USED = 2
    val FAIL = 3

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_register, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 配置宿舍号下拉栏
        val departmentList = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17)
        val departmentAdapter =
            ArrayAdapter(requireActivity(), R.layout.select_input_item, departmentList)
        (department.editText as AutoCompleteTextView).setAdapter(departmentAdapter)

        register.setOnClickListener {
            if (checkInput(listOf(account, password, name, department, telephone))) {
                // 关闭输入法
                (context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
                    name.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
                // 禁止按钮，防止多次发送
                register.isEnabled = false
                // 发起登录请求
                viewModel.register(
                    account.editText!!.text.toString().toLong(),
                    password.editText!!.text.toString(),
                    name.editText!!.text.toString(),
                    department.editText!!.text.toString().toInt(),
                    telephone.editText!!.text.toString()
                ).observe(viewLifecycleOwner) {
                    register.isEnabled = true
                    when (it.isSuccess) {
                        SUCCESS -> {
                            findNavController().popBackStack()
                        }
                        FAIL -> {
                            Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show()
                        }
                        ACCOUNT_USED -> {
                            Toast.makeText(context, "账号已被注册", Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            }
        }
    }

    private fun checkInput(viewList: List<TextInputLayout>): Boolean {
        var withoutEmpty = true
        for (view in viewList) {
            view.editText?.doOnTextChanged { text, _, _, _ ->
                if (text.toString() != "") view.error = null
            }
            if (view.editText?.text.toString() == "") {
                view.error = view.hint.toString() + "不能为空"
                withoutEmpty = false
            }
        }
        return withoutEmpty
    }
}